<?php
class visitController extends AController {
	// 回访首页查询
	public function indexAction() {
		return $this->render();
	}
	
	// 回访历史
	public function historyAction() {
		$pid = $this->request->getGetParameter('pid');
		$page = $this->request->getGetParameter('p', 1);
		$pageSize = 4;
		
		$totalCount = 0;
		$this->rows = JYDB::findAll('VISIT', "PATIENT_ID={$pid}", '*', 'VISIT_TIME DESC', $page, $pageSize, $totalCount);
		$this->pager = JYTools::pager("index.php?c=visit&a=history&pid={$pid}&p=_num_", $totalCount, $page, $pageSize);
		return $this->render();
	}
	
	public function detailAction() {
		$vid = $this->request->getGetParameter('vid');
		$this->row = JYDB::find('VISIT', "V_ID={$vid}");
		return $this->render();
	}
	
	public function visitinfoAction(){
		$page = $this->request->getGetParameter('p', 1);
		$pageSize = 10;
		$name = $this->request->getGetParameter('name');
		$status = $this->request->getGetParameter('status');
		$result = $this->request->getGetParameter('result');
		$again = $this->request->getGetParameter('again');
		$fail = $this->request->getGetParameter('fail');
		if(isset($_POST['sel_sub'])){
			unset($again);
			unset($fail);
			$name = $this->request->getPostParameter('name');
			$status = $this->request->getPostParameter('status');
			$result = $this->request->getPostParameter('result');
		}
		$totalCount = 0;
		if(isset($_POST['sel_again']) || $again){
			unset($name);
			unset($status);
			unset($result);
			unset($fail);
			if($_POST['sel_again']){
				$page = 1;
			}
			$again = 1;
			$where .= 'v.visit_state in (\'C\',\'H\',\'G\',\'I\')';
		}
		if(isset($_POST['sel_fail']) || $fail){
			unset($name);
			unset($status);
			unset($result);
			unset($again);
			unset($where);
			if($_POST['sel_fail']){
				$page = 1;
			}
			$fail = 1;
			$where .= 'v.visit_state in (\'D\',\'E\',\'F\',\'J\',\'K\')';
		}
		$where = $where?$where." and v.status is null":'v.status is null';
		if($_SESSION['user']['doc_no'] > 0){
			$where .= " and v.USER_ID=".CSM_USER_ID;
		}
		if(!empty($name)){
			unset($fail);
			unset($again);
			$where .= ' and (v.target_user like \'%'.$name.'%\' or v.patient_name like \'%'.$name.'%\')';
		}
		if(!empty($status)){
			unset($fail);
			unset($again);
			$where .= ' and v.visit_state=\''.$status.'\'';
		}
		if(!empty($result)){
			unset($fail);
			unset($again);
			$where .= ' and v.visit_result='.$result;
		}
		$sql = "select v.*,z.cy_dep
				  from VISIT v
				  left join zhuyuan z on z.biz_code=v.biz_code
				 where {$where}
				 order by v.VISIT_TIME DESC";
		//$this->rows = JYDB::findAll('VISIT', $where, '*', 'VISIT_TIME DESC', $page, $pageSize, $totalCount);
		$this->rows = JYDB::query($sql,$page, $pageSize, $totalCount);
		$this->pager = JYTools::pager("index.php?c=visit&a=visitinfo&pid={$pid}&p=_num_&name={$name}&status={$status}&result={$result}&again={$again}&fail={$fail}", $totalCount, $page, $pageSize);
		$this->name = $name;
		$this->sta = $status;
		$this->ret = $result;
		return $this->render();
	}
	public function delAction(){
		$id = $this->request->getGetParameter('id');
		if(JYDB::update('VISIT','V_ID='.$id,array('status'=>0))){
			echo '<script>history.back();</script>';exit;
		}
		$this->alert('系统繁忙，请稍后再试');exit;
	}
	public function editAction(){
		$id = $this->request->getGetParameter('id');
		if(isset($_POST['save_visit'])){
			$upd['VISIT_STATE'] = $this->request->getPostParameter('status');
			$upd['VISIT_RESULT'] = $this->request->getPostParameter('result');
			$upd['CONTENT'] = $this->request->getPostParameter('content');
			$upd['IS_TRACK'] = $this->request->getPostParameter('track');
			if(JYDB::update('VISIT','V_ID='.$id,$upd)){
				$this->alert('修改成功');exit;
			}
			$this->alert('系统繁忙');exit;
		}
		$visitinfo = $this->getVisitInfo($id);
		$this->visitinfo = $visitinfo;
		return $this->render();
	}
	
	// 回访保存
	public function resultAction() {
		// 获取参数
		$status = $this->request->getPostParameter('status');
		$result = '';
		if ($status) {
			$result = $this->request->getPostParameter('result');
		}
		$content = $this->request->getPostParameter('content');
		$track = $this->request->getPostParameter('track', '0');
		$visitType = $this->request->getPostParameter('type');
		$rule = $this->request->getPostParameter('rule');
		$bizCode = $this->request->getPostParameter('bizcode');
		$sysId = $this->request->getPostParameter('sysid');
		$visitCode = CSM_UNIT_ID.'_'.$_SESSION['user']['doc_no'].'_'.$bizCode;
		$visitTime = date('Y-m-d h:i:s');
		$patientId = $this->request->getPostParameter('pid');
		$patientName = $this->request->getPostParameter('pname');
		$targetUser = $this->request->getPostParameter('targetuser');
		$phone = $this->request->getPostParameter('phone');
		$depNo = $this->request->getPostParameter('dep');
		$docNo = $this->request->getPostParameter('doc');
		
		// 获取针对本次业务的回访次数
		$visitTimes = JYVisit::getVisitTimes($bizCode) + 1;
		
		$row = array('VISIT_CODE'=>$visitCode,
				'VISIT_TIME'=>$visitTime,
				'VISIT_TYPE'=>$visitType,
				'USER_ID'=>CSM_USER_ID,
				'USER_NAME'=>CSM_USER_NAME,
				'UNIT_ID'=>CSM_UNIT_ID,
				'USER_NO'=>$_SESSION['user']['doc_no'],
				'VISITRULE_ID'=>$rule,
				'VISIT_RESULT'=>$result,
				'VISIT_STATE'=>$status,
				'CONTENT'=>$content,
				'IS_THANK'=>'0',
				'IS_TRACK'=>$track,
				'SYS_ID'=>$sysId,
				'BIZ_CODE'=>$bizCode,
				'VISIT_TIMES'=>$visitTimes,
				'TARGET_USER'=>$targetUser,
				'VISIT_PHONE'=>$phone,
				'PATIENT_ID'=>$patientId,
				'PATIENT_NAME'=>$patientName);
	
		$rs = JYDB::create('VISIT', $row, 'V_ID');
		$trackResult = true;
		if ($rs and $track == '1') {
			$trackRow = array('VISIT_ID'=>$rs,
					'IS_TRACK'=>'1',
					'PATIENT_ID'=>$patientId,
					'NAME'=>$targetUser,
					'PHONE'=>$phone,
					'FEED_TIME'=>$visitTime,
					'CONTENT'=>$content,
					'STATE'=>'0',
					'TYPE'=>'5',
					'UNIT_ID'=>CSM_UNIT_ID,
					'DEP_NO'=>$depNo,
					'DOC_NO'=>$docNo);
			$trackResult = JYDB::create('FEEDBACK', $trackRow, 'F_ID');
		}
		if (!$rs or !$trackResult) {
			return json_encode(array('code'=>'0', 'msg'=>'回访结果保存失败'));
		} else {
			$_SESSION['monthaccess'] += 1;
			$_SESSION['dayaccess'] += 1;
			return json_encode(array('code'=>'1', 'msg'=>'回访结果保存成功'));
		}
	}
	
	function getVisitInfo($id){
		return JYDB::find('VISIT','V_ID='.$id);
	}
}